package com.scalar.chiptrack.operations.dataentry.lotsplit.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.lotsplit.forms.LotSplitForm;
import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;

import java.util.HashMap;
import java.util.ArrayList;
import java.sql.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 28, 2006
 * Time: 9:50:16 PM
 */
public class LotSplitLoadLotDetailsCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/lotSplit.do" ;


    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        LotSplitForm lotSplitForm = (LotSplitForm) form;
        HashMap errors                          = new HashMap();
        HashMap info                            = new HashMap();
        String action                           = request.getParameter("action");
        DBTransaction dbTransaction             =  null;

        String psType;

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        if( lotSplitForm.getFabLotNo().equalsIgnoreCase("-1"))
        {
            lotSplitForm.setLotId("0");
        }

        ArrayList lotList;
        ArrayList locationList;
        ArrayList processTypeList;
        String psTypeName;
        Lot lot;
        VendorLocation location;

        ArrayList lotSplitPartNumbersLableValueBeans     = new ArrayList();
        ArrayList lotSplitLocationsLableValueBeans       = new ArrayList();
        ArrayList lotSplitLotNumberLabelValueBeans       = new ArrayList();

        try
        {

            psType = lotSplitForm.getProcessType().substring(0, lotSplitForm.getProcessType().indexOf("/"));
            //lotSplitForm.setPsType( psType );
            lotSplitPartNumbersLableValueBeans = NewProductDAO.loadPartNumbers( null, 0, psType, true, true, true, 0 );
            psTypeName =lotSplitForm.getProcessType().substring(lotSplitForm.getProcessType().indexOf("/") + 1, lotSplitForm.getProcessType().length() );
            locationList= VendorDAO.getInventoryLocations(psTypeName);

            for (int i = 0; i < locationList.size(); i++)
            {
                location = (VendorLocation) locationList.get(i);
                lotSplitLocationsLableValueBeans.add(new org.apache.struts.util.LabelValueBean(location.getLocationName(), location.getLocationID()));
            }

            lotList = DataEntryDAO.getRunningandHoldLotNumbersByPSTypeName( lotSplitForm.getProcessStepId(), lotSplitForm.getLocationId(), null );
            for (int i = 0; i < lotList.size(); i++)
            {
                lot = (Lot) lotList.get(i);
                lotSplitLotNumberLabelValueBeans.add(new org.apache.struts.util.LabelValueBean( lot.getLotNumber(), lot.getLotId()));
            }

            lotSplitForm.setSplitLotDetails( null );
            LotDetails splitLotDetails = null;

            //splitLotDetails = DataEntryDAO.getLotInfoByPsType( lotSplitForm.getLotId(), psTypeName, psType, 0, 0, 0 );

            if( splitLotDetails.getDateIn() == null )
            {
                splitLotDetails.setDateIn( StringUtils.formatDateToString( new Date(System.currentTimeMillis()), "MM/dd/yyyy"));
            }

            splitLotDetails.setSplitLotOne(splitLotDetails.getLotNumber() + "A");
            splitLotDetails.setSplitLotTwo(splitLotDetails.getLotNumber() + "B");
            lotSplitForm.setSplitLotDetails( splitLotDetails );

            request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false));
            request.setAttribute("partNumbersList", lotSplitPartNumbersLableValueBeans);
            request.setAttribute("locationList", lotSplitLocationsLableValueBeans);
            request.setAttribute("splitLotNoList", lotSplitLotNumberLabelValueBeans);

            request.setAttribute("action", action);
            request.setAttribute("psType", psType);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return mapping.findForward("lotSplit");
    }
}
